home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / chown / RCS / chown.c,v < prev   
Encoding:
Text File  |  1988-07-22  |  4.3 KB  |  263 lines

  1. head     1.2;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.2
  9. date     88.07.22.10.20.07;  author ouster;  state Exp;
  10. branches ;
  11. next     1.1;
  12.  
  13. 1.1
  14. date     88.07.22.10.16.44;  author ouster;  state Exp;
  15. branches ;
  16. next     ;
  17.  
  18.  
  19. desc
  20. @@
  21.  
  22.  
  23. 1.2
  24. log
  25. @Forward declarations (to keep gcc happy).
  26. @
  27. text
  28. @/*
  29.  * Copyright (c) 1988 Regents of the University of California.
  30.  * All rights reserved.
  31.  *
  32.  * Redistribution and use in source and binary forms are permitted
  33.  * provided that this notice is preserved and that due credit is given
  34.  * to the University of California at Berkeley. The name of the University
  35.  * may not be used to endorse or promote products derived from this
  36.  * software without specific prior written permission. This software
  37.  * is provided ``as is'' without express or implied warranty.
  38.  */
  39.  
  40. #ifndef lint
  41. char copyright[] =
  42. "@@(#) Copyright (c) 1988 Regents of the University of California.\n\
  43.  All rights reserved.\n";
  44. #endif /* not lint */
  45.  
  46. #ifndef lint
  47. static char sccsid[] = "@@(#)chown.c    5.10 (Berkeley) 4/23/88";
  48. #endif /* not lint */
  49.  
  50. #include <sys/param.h>
  51. #include <sys/stat.h>
  52. #include <sys/dir.h>
  53. #include <pwd.h>
  54. #include <grp.h>
  55. #include <stdio.h>
  56. #include <ctype.h>
  57.  
  58. static int ischown, uid, gid, fflag, rflag, retval;
  59. static char *gname, *myname;
  60.  
  61. /*
  62.  * Forward declarations, to keep gcc happy:
  63.  */
  64.  
  65. extern int setgid(), setuid(), change(), chownerr(), err(), usage();
  66.  
  67. main(argc, argv)
  68.     int argc;
  69.     char **argv;
  70. {
  71.     extern char *optarg;
  72.     extern int optind;
  73.     register char *cp;
  74.     int ch;
  75.     char *index(), *rindex();
  76.  
  77.     myname = (cp = rindex(*argv, '/')) ? cp + 1 : *argv;
  78.     ischown = myname[2] == 'o';
  79.  
  80.     while ((ch = getopt(argc, argv, "Rf")) != EOF)
  81.         switch((char)ch) {
  82.         case 'R':
  83.             rflag++;
  84.             break;
  85.         case 'f':
  86.             fflag++;
  87.             break;
  88.         case '?':
  89.         default:
  90.             usage();
  91.         }
  92.     argv += optind;
  93.     argc -= optind;
  94.  
  95.     if (argc < 2)
  96.         usage();
  97.  
  98.     if (ischown) {
  99.         if (cp = index(*argv, '.')) {
  100.             *cp++ = '\0';
  101.             setgid(cp);
  102.         }
  103.         else
  104.             gid = -1;
  105.         setuid(*argv);
  106.     }
  107.     else {
  108.         uid = -1;
  109.         setgid(*argv);
  110.     }
  111.  
  112.     while (*++argv)
  113.         change(*argv);
  114.     exit(retval);
  115. }
  116.  
  117. static
  118. setgid(s)
  119.     register char *s;
  120. {
  121.     struct group *gr, *getgrnam();
  122.  
  123.     if (!*s) {
  124.         gid = -1;            /* argument was "uid." */
  125.         return;
  126.     }
  127.     for (gname = s; *s && isdigit(*s); ++s);
  128.     if (!*s)
  129.         gid = atoi(gname);
  130.     else {
  131.         if (!(gr = getgrnam(gname))) {
  132.             if (fflag)
  133.                 exit(0);
  134.             fprintf(stderr, "%s: unknown group id: %s\n",
  135.                 myname, gname);
  136.             exit(-1);
  137.         }
  138.         gid = gr->gr_gid;
  139.     }
  140. }
  141.  
  142. static
  143. setuid(s)
  144.     register char *s;
  145. {
  146.     struct passwd *pwd, *getpwnam();
  147.     char *beg;
  148.  
  149.     if (!*s) {
  150.         uid = -1;            /* argument was ".gid" */
  151.         return;
  152.     }
  153.     for (beg = s; *s && isdigit(*s); ++s);
  154.     if (!*s)
  155.         uid = atoi(beg);
  156.     else {
  157.         if (!(pwd = getpwnam(beg))) {
  158.             if (fflag)
  159.                 exit(0);
  160.             fprintf(stderr, "chown: unknown user id: %s\n", beg);
  161.             exit(-1);
  162.         }
  163.         uid = pwd->pw_uid;
  164.     }
  165. }
  166.  
  167. static
  168. change(file)
  169.     char *file;
  170. {
  171.     register DIR *dirp;
  172.     register struct direct *dp;
  173.     struct stat buf;
  174.  
  175.     if (chown(file, uid, gid)) {
  176.         chownerr(file);
  177.         return;
  178.     }
  179.     if (!rflag)
  180.         return;
  181.     if (lstat(file, &buf)) {
  182.         err(file);
  183.         return;
  184.     }
  185.     if ((buf.st_mode & S_IFMT) == S_IFDIR) {
  186.         if (chdir(file) < 0 || !(dirp = opendir("."))) {
  187.             err(file);
  188.             return;
  189.         }
  190.         for (dp = readdir(dirp); dp; dp = readdir(dirp)) {
  191.             if (dp->d_name[0] == '.' && (!dp->d_name[1] ||
  192.                 dp->d_name[1] == '.' && !dp->d_name[2]))
  193.                 continue;
  194.             change(dp->d_name);
  195.         }
  196.         closedir(dirp);
  197.         if (chdir("..")) {
  198.             err("..");
  199.             exit(fflag ? 0 : -1);
  200.         }
  201.     }
  202. }
  203.  
  204. static
  205. chownerr(file)
  206.     char *file;
  207. {
  208.     static int euid = -1, ngroups = -1;
  209.  
  210.     /* check for chown without being root */
  211.     if (uid != -1 && euid == -1 && (euid = geteuid())) {
  212.         if (fflag)
  213.             exit(0);
  214.         err(file);
  215.         exit(-1);
  216.     }
  217.     /* check group membership; kernel just returns EPERM */
  218.     if (gid != -1 && ngroups == -1) {
  219.         int groups[NGROUPS];
  220.  
  221.         ngroups = getgroups(NGROUPS, groups);
  222.         while (--ngroups >= 0 && gid != groups[ngroups]);
  223.         if (ngroups < 0) {
  224.             if (fflag)
  225.                 exit(0);
  226.             fprintf(stderr,
  227.                 "%s: you are not a member of group %s.\n",
  228.                 myname, gname);
  229.             exit(-1);
  230.         }
  231.     }
  232.     err(file);
  233. }
  234.  
  235. static
  236. err(s)
  237.     char *s;
  238. {
  239.     if (fflag)
  240.         return;
  241.     fprintf(stderr, "%s: ", myname);
  242.     perror(s);
  243.     retval = -1;
  244. }
  245.  
  246. static
  247. usage()
  248. {
  249.     fprintf(stderr, "usage: %s [-Rf] %s file ...\n", myname,
  250.         ischown ? "owner[.group]" : "group");
  251.     exit(-1);
  252. }
  253. @
  254.  
  255.  
  256. 1.1
  257. log
  258. @Initial revision
  259. @
  260. text
  261. @d34 6
  262. @
  263.